package com.nsoft.utils;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.SortedMap;
import java.util.TreeMap;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.webkit.WebView;
import android.widget.ViewFlipper;

import com.nsoft.objects.ChapterInfo;
import com.nsoft.objects.PageInfo;

public class viewengine {
	
	private static SortedMap<Integer,ChapterInfo> 
			chapList=new TreeMap<Integer,ChapterInfo>();
	
	public static View[] createViews(Context context){
		ArrayList<View> viewList=new ArrayList<View>();
		ChapterInfo[] chaps=chapList.values().
					toArray(new ChapterInfo[]{});
		
		View view=null;
		PageInfo[][] page2DList;
		PageInfo[] vertPageList;
		for (int i=0;i<chaps.length;i++){
			page2DList=chaps[i].get2DPageList();
			for (int j=0;j<page2DList.length;j++){
				vertPageList=page2DList[j];
				if (vertPageList!=null){
//					if (vertPageList.length==1){
						view=createWebView(context);
						((WebView)view).loadUrl(
							Constant.URL_PREFIX+
							vertPageList[0].getPath());
//					}else{
//						view=new ViewFlipper(context);
//					}
					viewList.add(view);
				}
			}
		}
		
		return viewList.toArray(new View[]{});
	}
	
	@SuppressLint("SetJavaScriptEnabled")
	private static WebView createWebView(Context ctx){
		WebView wv=new WebView(ctx);
		wv.getSettings().setLoadWithOverviewMode(true);
		wv.getSettings().setUseWideViewPort(true);
		wv.getSettings().setBuiltInZoomControls(true);
		wv.getSettings().setJavaScriptEnabled(true);
		wv.setScrollBarStyle(
				WebView.SCROLLBARS_OUTSIDE_OVERLAY);
		wv.setInitialScale(getScale(ctx,wv));
		return wv;
	}
	
	public static void getBookFromBookConfig(
						String configFilePath){
	
		String bookPath=configFilePath.substring(0,
			configFilePath.lastIndexOf(Constant.PATH_SPLITTER));
		chapList=new TreeMap<Integer,ChapterInfo>();
		chapList.clear();
		
		try{
			DocumentBuilderFactory docBuilderFactory=
						DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuilder=docBuilderFactory.
								newDocumentBuilder();
			FileInputStream fis=new 
					FileInputStream(configFilePath);
			Document doc=docBuilder.parse(fis);
			
			NodeList list=doc.getElementsByTagName(
						Constant.xml.XML_CONFIG_TAG_PAGE);
		
			NodeList c_nodes=null; 
			ChapterInfo chInfo=null;
			int chapIndex=-1;
			PageInfo pInfo=null;
			String nodeCont=Constant.EMPTY,nodeName;
			boolean isPageGotBugs=false;
			for (int i=0;i<list.getLength();i++){
				c_nodes=list.item(i).getChildNodes();
				pInfo=new PageInfo();
				isPageGotBugs=false;
				
				for (int j=0;j<c_nodes.getLength();j++){
					nodeCont=c_nodes.item(j).getTextContent();
					nodeName=c_nodes.item(j).getNodeName();
					if (nodeName.equalsIgnoreCase(
						Constant.xml.XML_CONFIG_TAG_PAGEID)){
						if (nodeCont.equals(Constant.EMPTY)){
							isPageGotBugs=true;
							break;
						}
						pInfo.setId(nodeCont);
					}else if (nodeName.equalsIgnoreCase(
						Constant.xml.XML_CONFIG_TAG_PAGEINDEX)){
						if (nodeCont.equals(Constant.EMPTY)){
							isPageGotBugs=true;
							break;
						}
						chapIndex=Integer.parseInt(
								nodeCont.substring(0,2))-1;
						pInfo.setChapter(chapIndex);
						pInfo.setChapterName(
								nodeCont.substring(0,2));
						pInfo.sethIndex(Integer.parseInt(
								nodeCont.substring(2,4))-1);
						pInfo.setvIndex(Integer.parseInt(
								nodeCont.substring(4,5))-1);
					}else if (nodeName.equalsIgnoreCase(
						Constant.xml.XML_CONFIG_TAG_PAGENAME)){
						if (nodeCont.equals(Constant.EMPTY)){
							isPageGotBugs=true;
							break;
						}
						pInfo.setName(nodeCont);
					}
				}
				if (!isPageGotBugs){
					pInfo.autoSetPath(bookPath);
					if (!chapList.containsKey(chapIndex)){
						chInfo=new ChapterInfo(chapIndex);
						chapList.put(chInfo.getId(),chInfo);
					}else{
						chInfo=chapList.get(chapIndex);
					}
					chInfo.addPage(pInfo);
				}
			}
			
			fis.close();
		}catch(Exception e){
			Log.v("engine.getPageFromConfigFile()",
				e.getClass()+": "+e.getMessage());
		}
	}
	
	private static int getScale(Context ctx,WebView wv){
		Display display = ((WindowManager)ctx.
	    	getSystemService(Context.
	    		WINDOW_SERVICE)).getDefaultDisplay(); 
		@SuppressWarnings("deprecation")
		int width = display.getWidth(); 
	    Double val = (double)width/
	    		(double)Constant.pagesize.WIDTH;
	    val = val * 100d;
	    return val.intValue();
	}

}
